package com.cindata

import com.cindata.dictionary.ImportFileTypeDict
import com.cindata.dictionary.EvaluateDict
import com.cindata.dictionary.ExecStatusDict
import groovy.transform.ToString

@ToString(includes = ["batchNumber", "estateNum", "matchNum"], includeNames = true, includePackage = false)
class EstateImportBatch
{
    def springSecurityService

    String batchNumber
    ImportFileTypeDict importFileType

    Account account

    Integer estateNum = 0
    Integer matchNum = 0

    ExecStatusDict status

    Integer execProgress
    //执行进度

    EvaluateDict evaluateType
    //执行进度

    Account latestEvaluateAccount

    String type

    Date createdDate = new Date()
    Date modifiedDate = new Date()
    User createBy
    User modifyBy
    Integer isDeleted = 0

    Date startTime
    Date endTime

    // 分发策略
    Component distributeRule
    //评估机构
    static hasMany = [accounts: BatchAccount]

    static mapping = {
        autowire true
    }
    static constraints = {

        type maxSize: 20, nullable: true, blank: true
        type inList: ['导入', '评估', '不良']

        batchNumber maxSize: 128, nullable: true, blank: true
        importFileType maxSize: 2, nullable: true, blank: true
        estateNum maxSize: 10, nullable: true, blank: true
        matchNum maxSize: 10, nullable: true, blank: true
        status maxSize: 10, nullable: true, blank: true

        execProgress nullable: true, blank: true
        account nullable: true, blank: true

        status nullable: true, blank: true

        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true
        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true
        isDeleted maxSize: 5, nullable: true, blank: true
        evaluateType nullable: true, blank: true
        
        latestEvaluateAccount nullable: true, blank: true
        distributeRule nullable: true, blank: true

        startTime nullable: true, blank: true
        endTime nullable: true, blank: true
    }


    def beforeInsert()
    {
        def user = springSecurityService?.currentUser
        createBy = user
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }


    // def updateBatchCount()
    // {
    //     def successCount = EstateImport.countByBatchAndEvaluateStatue(this, ExecStatusDict.SUCCESS())
    //     def failureCount = EstateImport.countByBatchAndEvaluateStatue(this, ExecStatusDict.FAILURE())
    //     this.matchNum = successCount + failureCount
    //     if (successCount + failureCount == this.estateNum)
    //     {
    //         if (failureCount == this.estateNum)
    //         {
    //             this.status = ExecStatusDict.FAILURE()
    //         }
    //         else if (failureCount > 0)
    //         {
    //             this.status = ExecStatusDict.PARTFAILURE()
    //         }
    //         else
    //         {
    //             this.status = ExecStatusDict.SUCCESS()
    //         }
    //     }
    //     this.save()
    // }

    // def updateBatchEstateCount()
    // {
    //     def successCount = BatchEstate.countByBatchAndEvaluateStatue(this, ExecStatusDict.SUCCESS())
    //     def failureCount = BatchEstate.countByBatchAndEvaluateStatue(this, ExecStatusDict.FAILURE())

    //     this.matchNum = successCount + failureCount
    //     println "successCount:" + successCount
    //     println "failureCount:" + failureCount
    //     println "estateNum:" + this.estateNum
    //     if (successCount + failureCount == this.estateNum)
    //     {
    //         if (successCount == this.estateNum)
    //         {
    //             this.status = ExecStatusDict.SUCCESS()
    //         }
    //         else if (failureCount == this.estateNum)
    //         {
    //             this.status = ExecStatusDict.FAILURE()
    //         }
    //         else if (failureCount > 0)
    //         {
    //             this.status = ExecStatusDict.PARTFAILURE()
    //         }
    //         else
    //         {
    //             this.status = ExecStatusDict.EXECUTING()
    //         }
    //     }
    //     this.modifiedDate = new Date()
    //     this.save()
    // }
}
